package impls.graphics;

import java.awt.Choice;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.Iterator;
import java.util.LinkedList;

import javax.swing.JFrame;

/**
 * taken from snake game
 * 
 * @author thailow
 * 
 */
class Screen extends JFrame implements KeyListener, FocusListener,
		WindowListener, Runnable, ItemListener {
	private static final long serialVersionUID = 6114093581351645054L;

	private static final int SCORESLOTS = 5;

	private static final Color DEEPSKYBLUECOLOR = new Color(0x00bfbf);

	private Thread anima;

	private int textHeight, textWidth;

	private ControlCenter cc;

	private Graphics g;

	private Choice carAgent;

	private Choice stationAgent;

	private TextField fuel;

	private TextField price;

	private TextArea messages;

	private LinkedList<String> messagesList;

	// Constructor
	Screen(String title, ControlCenter control, int delay, int width, int height) {
		super();
		this.cc = control;
		anima = null;
		messagesList = new LinkedList<String>();

		setTitle(title);
		setResizable(false);
		// setBackground(Color.white);

		// New Stuff
		Panel left = new Panel();
		left.setBackground(Color.white);

		Panel right = new Panel();
		Panel interior = new Panel(new GridLayout(2, 2));
		Panel interior2 = new Panel(new FlowLayout());
		right.setLayout(new GridLayout(2, 1));

		carAgent = new Choice();
		carAgent.addItemListener(this);

		stationAgent = new Choice();
		stationAgent.addItemListener(this);

		fuel = new TextField("fuel");
		fuel.setColumns(5);
		fuel.setEditable(false);

		price = new TextField("price");
		price.setColumns(5);
		price.setEditable(false);

		interior.add(carAgent);
		interior.add(stationAgent);
		interior.add(fuel);
		interior.add(price);

		messages = new TextArea();
		messages.setEditable(false);
		messages.setRows(5);
		// messages.setColumns(20);

		interior2.add(new Label("Messages:"));
		interior2.add(messages);

		right.add(interior);
		right.add(interior2);

		// Main Panel

		GridLayout gMain = new GridLayout(1, 2);
		gMain.setHgap(20);
		gMain.setVgap(20);

		setLayout(gMain);

		add("panel1", left);
		add("child", right);

		addFocusListener(this);
		addKeyListener(this);
		addWindowListener(this);

		// setBounds(0, 0, width*2, height+45); //width + 118, height + 48
		// this.pack();
		setSize(new Dimension(width * 3 + 50, height + 150));
		setVisible(true);
		g = left.getGraphics();
		textHeight = (int) (getFont().getMaxCharBounds(
				((Graphics2D) g).getFontRenderContext()).getHeight() + 3);
		textWidth = width / SCORESLOTS;
	}

	// Public Services
	public void StartAnimations() {
		anima = new Thread(this);
		anima.start();
	}

	public Graphics GetGraphics() {
		return g;
	}

	public void ScoreAt(int x, String s) {
		if (x < 0 || x >= SCORESLOTS)
			throw new Error("ScoreAt: " + x + " is outside the valid range");
		g.setColor(Color.white);
		g.fillRect(x * textWidth, -textHeight, textWidth, textHeight);
		g.setColor(DEEPSKYBLUECOLOR);
		g.drawString(s, x * textWidth, -3);
	}

	public void focusGained(FocusEvent evt) {
	}

	public void focusLost(FocusEvent evt) {
	}

	public synchronized void keyTyped(KeyEvent evt) {
	}

	public synchronized void keyPressed(KeyEvent evt) {
	}

	public void keyReleased(KeyEvent evt) {
	}

	public synchronized void paint(Graphics g) {
		if (anima != null)
			cc.redrawAll();
	}

	public void windowActivated(WindowEvent e) {
	}

	public void windowClosed(WindowEvent e) {
	}

	public synchronized void windowClosing(WindowEvent e) {
		cc.end();
	}

	public void windowDeactivated(WindowEvent e) {
	}

	public void windowDeiconified(WindowEvent e) {
	}

	public void windowIconified(WindowEvent e) {
	}

	public void windowOpened(WindowEvent e) {
	}

	public void run() {
	}

	public Choice getStationAgent() {
		return stationAgent;
	}

	public Choice getCarAgent() {
		return carAgent;
	}

	public void writeMessage(String m) {
		if (messagesList.size() < 9) {
			messagesList.add(m);
		} else {
			messagesList.remove(0);
			messagesList.add(m);
		}
		Iterator<String> it = messagesList.iterator();
		String post = "";
		while (it.hasNext()) {
			post = post + it.next() + "\n";
		}

		messages.setText(post);
	}

	public void itemStateChanged(ItemEvent e) {
		Choice c = (Choice) e.getItemSelectable();

		if (c.getName().equals("choice0")) {
			fuel.setText(cc.getFuel(c.getSelectedIndex()));
		}
		if (c.getName().equals("choice1")) {
			price.setText(cc.getPrice(c.getSelectedIndex()));
		}

	}

	public void setFuel(String fuel) {
		this.fuel.setText(fuel);
	}

	public void setPrice(String price) {
		this.price.setText(price);
	}

}
